﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ml_testing_translate.MS;

namespace ml_testing_translate
{
    public partial class Form1 : Form
    {
        private const string MEMO_KEY = "C1D1CDEB53FDFCC57E7F911E872CD85B0ACF46E3", FN_TRANS_CONTEXT="TransContext.txt";

        private const string LAN_From_Key = "zh-CHS", LAN_To_Key="en";



        public Form1()
        {
            InitializeComponent();
            cmbFrom.SelectedIndex = 1;
            cmbTo.SelectedIndex = 0;
        }

        private string GetTransValue(string srcVal)
        {
            var client = new LanguageServiceClient();
            var options = new TranslateOptions();
            options.Category = "general";
            options.ContentType = "text/plain";
            options.User = "martin.lai.cn@hotmail.com";
            var reVal = client.GetTranslations(MEMO_KEY, srcVal, cmbFrom.SelectedItem.ToString(), cmbTo.SelectedItem.ToString(), 1, options);
            var sb = new StringBuilder();

            foreach (var trans in reVal.Translations)
            {
                sb.Append(trans.TranslatedText);
            }
            
            return sb.ToString();
        }

        private string GetTransValue(string[] ssVal)
        {
            var client = new LanguageServiceClient();
            var options = new TranslateOptions();
            options.Category = "general";
            options.ContentType = "text/plain";
            options.User = "martin.lai.cn@hotmail.com";
            var reVal = client.GetTranslationsArray(MEMO_KEY, ssVal, cmbFrom.SelectedItem.ToString(), cmbTo.SelectedItem.ToString(), 1, options);
            var sb = new StringBuilder();
            foreach(var ss in reVal)
            {
                foreach(var trans in ss.Translations)
                {
                    sb.Append(trans.TranslatedText);
                }
                sb.Append("\r\n");
            }
            return sb.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetTransValue(txt.Text));
        }

        private void btnRunTranslateForFile_Click(object sender, EventArgs e)
        {
            var rowAmount = 0;
            var charAmount = 0;
            if(File.Exists(FN_TRANS_CONTEXT))
            {
                var startDate = DateTime.Now;
                var sb = new StringBuilder();
                var sr = new StreamReader(FN_TRANS_CONTEXT);
                try
                {
                    while (sr.Peek() > 0)
                    {
                        var lineVal = sr.ReadLine();
                        if (string.IsNullOrEmpty(lineVal))
                        {
                            sb.Append("\r\n");
                            continue;
                        }
                        charAmount += lineVal.Length;
                        var transVal = GetTransValue(lineVal);
                        sb.Append(transVal+"\r\n");
                        rowAmount++;
                    }
                    if(rowAmount > 0)
                    {
                        sb.Append("\r\n\r\nTranslate chars amount : [" + rowAmount + "]");
                        sb.Append("\r\n\r\nUSE TIMES : [" + (DateTime.Now - startDate).TotalMilliseconds + "]ms ");
                    }
                }
                finally
                {
                    sr.Dispose();
                    sr.Close();
                }
                if (rowAmount> 0) MessageBox.Show(sb.ToString());
            }
        }
        private const char FG_RowVal='┱';
        private void btnRunTranslatesForFile_Click(object sender, EventArgs e)
        {
            StringCollection sc = new StringCollection();
            var sb = new StringBuilder();
            if (File.Exists(FN_TRANS_CONTEXT))
            {
                var startDate = DateTime.Now;
                var sr = new StreamReader(FN_TRANS_CONTEXT);
                try
                {
                    var context = sr.ReadToEnd();
                    var ss = context.Replace("\r\n", FG_RowVal.ToString()).Split(FG_RowVal);
                    
                    foreach(string str in ss)
                    {
                        if (str.Length > 0)
                            sc.Add(str);
                    }
                    if (sc.Count > 0)
                    {
                        var ssTrans = new string[sc.Count];
                        for (int i = 0; i < sc.Count; i++)
                            ssTrans[i] = sc[i];
                        var reTrans = GetTransValue(ssTrans);
                        sb.Append(reTrans + "\r\n\r\n");
                        sb.Append("\r\n\r\nTranslate chars amount : [" + (context.Length - ss.Length) + "]");
                        sb.Append("\r\n\r\nUSE TIMES : [" + (DateTime.Now - startDate).TotalMilliseconds + "]ms ");
                    }
                }
                finally
                {
                    sr.Dispose();
                    sr.Close();
                }
                if (sb.Length > 0) MessageBox.Show(sb.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}